package com.controller;

import com.beans.ErrorStatusEnum;
import com.beans.FileManageEntity;
import com.common.entity.R;
import com.common.utils.UploadFileUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Date;
import java.util.List;


/**
 * 首页轮播图管理
 *
 * @author gm
 * @date 2020/6/5
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("index")
public class IndexController {

    @PersistenceContext
    private EntityManager entityManager;
    @Resource
    private UploadFileUtil uploadFileUtil;

    /**
     * 获取首页全部轮播图
     *
     * @return com.common.entity.R
     * @author gm
     * @date 2020/6/5
     */
    @GetMapping("getPicture")
    public R getPicture() {
        List resultList = entityManager.createQuery("from FileManageEntity where state = 1 and fileType = 1", FileManageEntity.class).getResultList();
        R ok = R.ok();
        ok.put("total", resultList.size());
        ok.put("rows", resultList);
        return ok;
    }

    /**
     * 上传首页轮播图
     *
     * @param fileName 文件名
     * @param message  文件描述
     * @param file     文件
     * @return com.common.entity.R
     * @author gm
     * @date 2020/6/5
     */
    @PostMapping("uploadPicture")
    public R uploadPicture(String fileName, String message, @RequestParam(value = "file") MultipartFile file) {
        //类型1为首页轮播图
        String fileType = "1";
        R r = uploadFileUtil.uploadPicture(file, fileType);
        if (ErrorStatusEnum.OK.code().equals(r.get(R.CODE))) {
            //文件访问url
            String url = (String) r.get(R.DATA);
            //组装FileManageEntity信息并保存到数据库
            FileManageEntity fileManage = new FileManageEntity();
            fileManage.setCreateTime(new Date());
            fileManage.setState(1);
            fileManage.setFileName(url.substring(url.lastIndexOf("/") + 1));
            fileManage.setFileType(Integer.valueOf(fileType));
            fileManage.setUrl(url);
            fileManage.setFileOldName(fileName);
            fileManage.setMessage(message);
            entityManager.merge(fileManage);
        }
        return r;
    }

    /**
     * 删除首页轮播图
     *
     * @param id 文件id
     * @return com.common.entity.R
     * @author gm
     * @date 2020/6/5
     */
    @PostMapping("deletePicture")
    public R deletePicture(Integer id) {
        List resultList = entityManager.createNativeQuery("select fileName from file_manage where id = :id").setParameter("id", id).getResultList();
        if (CollectionUtils.isEmpty(resultList)) {
            return R.error(ErrorStatusEnum.PARAMS_ILLEGAL.code(), "图片不存在，删除失败！");
        }
        String fileName = (String) resultList.get(0);
        //删除数据
        entityManager.createQuery("delete from FileManageEntity where id = :id ").setParameter("id", id).executeUpdate();
        //删除文件
        R r = null;
        try {
            r = uploadFileUtil.deletePicture((String) fileName, "1");
        } catch (Exception e) {
            e.printStackTrace();
            r = R.error(ErrorStatusEnum.UNKNOWN.code(), "文件删除失败！");
        }
        return r;
    }

}
